import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        
        toolBar=QToolBar()

        btn=QPushButton('MakeDir')
        btn.clicked.connect(self.MakeDir)
        toolBar.addWidget(btn)        

        btn=QPushButton('Remove')
        btn.clicked.connect(self.Remove)
        toolBar.addWidget(btn)

        layout.addWidget(toolBar)

        self.model=QFileSystemModel()
        self.model.setRootPath(self.parent.getAppDir())
        

        self.treeView=QTreeView()        
        self.treeView.setModel(self.model)
        #self.treeView.setRootIndex(self.model.index(self.parent.getAppDir()))
        layout.addWidget(self.treeView)

        self.setLayout(layout)
    def MakeDir(self):
        index = self.treeView.currentIndex()
        if index.isValid()==False:return
        dirName,ok=QInputDialog.getText(self,'Create Directory','Directory Name')       
        if dirName is not None:
            self.model.mkdir(index,dirName)
            self.parent.OnShowMessage(f'Create Directory:{dirName}')    
    
    def Remove(self):
        index = self.treeView.currentIndex()
        if index.isValid()==False:return
        path=self.model.filePath(index)
        if self.model.isDir(index):
            self.model.rmdir(index)
            self.parent.OnShowMessage(f'Remove Directory:{path}')            
        else:   
            self.model.remove(index)
            self.parent.OnShowMessage(f'Remove File:{path}')
            
    
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet